﻿namespace Blaze.Editor.CLI
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    public struct SvnInfo
    {
        public string Path;
        public string WorkingCopyRoot;
        public string Url;
        public string RelativeUrl;
        public string RepositoryRoot;
        public string RepositoryUUID;
        public int Revision;
        public string NodeKind;
        public string Schedule;
        public string LastChangedAuthor;
        public string LastChangedRevision;
        public string LastChangedDate;

        private static string getValue(Dictionary<string, string> map, string key, string alias = null)
        {
            string ret;
            if (map.TryGetValue(key, out ret))
                return ret;
            if (alias != null && map.TryGetValue(alias, out ret))
                return ret;
            return null;
        }

        public static SvnInfo Parse(string text)
        {
            var map = new Dictionary<string, string>();
            foreach (Match match in mRegex.Matches(text))
            {
                var key = match.Result("${key}");
                var value = match.Result("${value}");
                map.Add(key, value);
            }

            return new SvnInfo
            {
                Path = getValue(map, "Path", "路径"),
                WorkingCopyRoot = getValue(map, "Working Copy Root Path", "工作副本根目录"),
                Url = getValue(map, "URL"),
                RelativeUrl = getValue(map, "Relative URL"),
                RepositoryRoot = getValue(map, "Repository Root", "版本库根"),
                RepositoryUUID = getValue(map, "Repository UUID", "版本库 UUID"),
                Revision = int.Parse(getValue(map, "Revision", "版本")),
                NodeKind = getValue(map, "Node Kind", "节点种类"),
                Schedule = getValue(map, "Schedule", "调度"),
                LastChangedAuthor = getValue(map, "Last Changed Author", "最后修改的作者"),
                LastChangedRevision = getValue(map, "Last Changed Rev", "最后修改的版本"),
                LastChangedDate = getValue(map, "Last Changed Date", "最后修改的时间"),
            };
        }

        /// <summary>
        /// 返回该实例的完全限定类型名。
        /// </summary>
        /// <returns>
        /// 包含完全限定类型名的 <see cref="T:System.String"/>。
        /// </returns>
        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.AppendFormat("Path: {0}\r\n", Path);
            builder.AppendFormat("Working Copy Root Path: {0}\r\n", WorkingCopyRoot);
            builder.AppendFormat("URL: {0}\r\n", Url);
            builder.AppendFormat("Relative URL: {0}\r\n", RelativeUrl);
            builder.AppendFormat("Repository Root: {0}\r\n", RepositoryRoot);
            builder.AppendFormat("Repository UUID: {0}\r\n", RepositoryUUID);
            builder.AppendFormat("Revision: {0}\r\n", Revision);
            builder.AppendFormat("Node Kind: {0}\r\n", NodeKind);
            builder.AppendFormat("Schedule: {0}\r\n", Schedule);
            builder.AppendFormat("LastChangedAuthor: {0}\r\n", LastChangedAuthor);
            builder.AppendFormat("LastChangedRevision: {0}\r\n", LastChangedRevision);
            builder.AppendFormat("LastChangedDate: {0}\r\n", LastChangedDate);
            return builder.ToString();
        }

        private static readonly Regex mRegex = new Regex(@"(?<key>.+?): (?<value>.+?)" + Environment.NewLine);
    }
}